<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mrestaurantorder extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 
     * @void Country
     * 
     */
    
    public function getRetaurantDB($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        $sql  = '';
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (r.title like '%%".$key."%%' OR ";
            $sql .= " r.title_ascii like '%%".$key."%%') ";
        }else{
            $sql = "SELECT r.id, r.`name`, r.name_ascii, r.image, r.logo, r.active, r.admin_id, r.image_thumb, r.created FROM restaurant AS r WHERE 1=1 ";
        }
        $sql .= "ORDER BY r.id DESC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        
        foreach($xs AS $el=>$le)
        {
            $xs[$el]->tong = $this->db->query('SELECT id FROM booking WHERE restaurant_id = '.$le->id)->num_rows();
        }
        
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);   
    }
    
    public function getListOrderDB($number,$offset)
    {
        $limit = $offset ? intval($offset) : '0';
        $sql  = '';
        $id_restaurant = $this->getParamUri(4,1);
        $sql = "SELECT id, `name`, phone, peoples,time_booking, created, is_gpeople FROM booking WHERE 1=1 AND restaurant_id =".$id_restaurant." ";
        
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (name like '%%".$key."%%' OR ";
            $sql .= " phone like '%%".$key."%%') ";
        }
        
        $sql .= "ORDER BY id DESC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();

        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
    }
    
    public function viewOrderDB($id_order)
    {
        return $this->db->query('SELECT b.id, b.`name` AS bname, DATE_FORMAT(b.created, "%H:%i:%s %d/%m/%Y") AS time, b.phone, b.peoples, b.is_gpeople, b.email, b.time_booking, b.address_id, b.required, b.restaurant_id, ra.address, ra.phone AS raphone, ra.latitude, ra.longitude, ra.id_zone, b.created, z.title AS ztitle, c.title, r.`name` AS rname, r.logo, r.hotline FROM booking AS b INNER JOIN restaurant_address AS ra ON b.address_id = ra.id INNER JOIN zone AS z ON ra.id_zone = z.id INNER JOIN country AS c ON z.id_country = c.id INNER JOIN restaurant AS r ON b.restaurant_id = r.id WHERE b.id = '.$id_order)->row_object();
    }
    
}
